package pri.zey.custom.tools;/**
 * Created by Administrator on 2016/3/16.
 */

import android.app.ProgressDialog;
import android.content.Context;

/**
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>类描述
 * 对话框工具
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>属性描述
 * >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>方法描述
 * showProgress()---->显示默认进度对话框
 * showProgress(String msg)---->显示默认进度对话框（带文字）
 * closeProgress()---->关闭进度对话框
 */
public class Tdialog {

    private static Tdialog mDialog;
    private ProgressDialog mProgressDialog;
    private Context mContext;
    private TdialogModle theme;

    private Tdialog(Context mContext, TdialogModle theme){
        this.mContext = mContext;
        this.theme = theme;
    }

    public synchronized static Tdialog getInstance(Context mContext){
        if(mDialog == null){
            mDialog = new Tdialog(mContext, null);
        }
        return mDialog;
    }

    public synchronized static Tdialog getInstance(Context mContext, TdialogModle theme){
        if(mDialog == null){
            mDialog = new Tdialog(mContext, theme);
        }
        return mDialog;
    }

    /**
     * 构建默认对话框
     */
    public Tdialog BuidleProgress(){
        if(mProgressDialog == null)
            mProgressDialog = new ProgressDialog(mContext);
        if (theme != null){
            setTitle(theme.getTitle());
            setMsg(theme.getMsg());
            setProgressStyle(theme.getProgressStyle());
            setCanceledOnTouchOutside(theme.isCanceledOnTouchOutside());
        }else{
            setTitle("提示");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        }
        return mDialog;
    }

    /**
     * 标题
     */
    public void setTitle(Object Title){
        if(Title instanceof String)
            mProgressDialog.setTitle((String)Title);
        else if(Title instanceof Integer)
            mProgressDialog.setTitle((int)Title);
        else
            try {
                throw new TdialogException("Unable to identify the type!!!!");
            } catch (TdialogException e) {
                e.printStackTrace();
            }
    }

    /**
     * 显示文本
     */
    public void setMsg(String msg){
        mProgressDialog.setMessage(msg);
    }

    /**
     * 对话框类型
     * @param ProgressStyle
     */
    public void setProgressStyle(int ProgressStyle){
        mProgressDialog.setProgressStyle(ProgressStyle);
    }

    /**
     * 是否点击对话框以外的区域关闭对话框
     * @param CanceledOnTouchOutside
     */
    public void setCanceledOnTouchOutside(boolean CanceledOnTouchOutside){
        mProgressDialog.setCanceledOnTouchOutside(CanceledOnTouchOutside);
    }

    /**
     * 显示进度对话框
     */
    public void show(){
        if(mProgressDialog != null) {
            mProgressDialog.show();
            TL.d(this.toString(), "<<<<Show>>>>");
        }else {
            BuidleProgress();
            mProgressDialog.show();
            TL.e(this.toString(), "<<<<Show NULL>>>>");
        }
    }

    /**
     * 关闭进度对话框
     */
    public void close(){
        if(mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
            TL.d(this.toString(), "<<<<Close>>>>");
        }else{
            TL.e(this.toString(), "<<<<Close NULL>>>>");
        }

    }

}
